package cn.eeepay.boss.action.risk;

import cn.eeepay.boss.action.FunctionManagerAction;
import cn.eeepay.boss.system.DataSource;
import cn.eeepay.boss.system.SystemLog;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.FunctionManager;
import cn.eeepay.framework.model.risk.TransLimitRule;
import cn.eeepay.framework.model.risk.TransLimitWhite;
import cn.eeepay.framework.service.FunctionManagerService;
import cn.eeepay.framework.service.MerchantInfoService;
import cn.eeepay.framework.service.risk.TransLimitService;
import cn.eeepay.framework.util.ClientInterface;
import cn.eeepay.framework.util.Constants;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 *
 * @author yyao
 * @date 2020/8/27 9:59
 */
@Controller
@RequestMapping(value = "/transLimit")
public class TransLimitAction {

    private static final Logger log = LoggerFactory.getLogger(TransLimitAction.class);

    @Resource
    private TransLimitService transLimitService;
    @Resource
    private FunctionManagerService functionManagerService;
    @Resource
    private MerchantInfoService merchantInfoService;

    /**
     * 查询交易限制管理列表
     */
    @RequestMapping(value = "/selectTransLimitRuleList")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> selectTransLimitRuleList(@RequestParam("info") String param, @ModelAttribute("page")
            Page<TransLimitRule> page) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            TransLimitRule info = JSONObject.parseObject(param, TransLimitRule.class);
            transLimitService.selectTransLimitRuleList(info, page);
            msg.put("page",page);
            msg.put("status", true);
        } catch (Exception e){
            log.error("查询交易限制管理列表异常!",e);
            msg.put("status", false);
            msg.put("msg", "查询交易限制管理列表异常!");
        }
        return msg;
    }

    /**
     * 查询交易限制管理详情
     */
    @RequestMapping(value = "/selectTransLimitRuleDetail")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> selectTransLimitRuleDetail(@RequestParam("id") Long id) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            TransLimitRule data=transLimitService.selectTransLimitRuleDetail(id);
            msg.put("data",data);
            msg.put("status", true);
        } catch (Exception e){
            log.error("查询交易限制管理详情异常!",e);
            msg.put("status", false);
            msg.put("msg", "查询交易限制管理详情异常!");
        }
        return msg;
    }

    /**
     * 限额新增
     */
    @RequestMapping(value = "/addTransLimitRule")
    @ResponseBody
    @SystemLog(description = "限额新增",operCode="transLimit.addTransLimitRule")
    public Map<String,Object> addTransLimitRule(@RequestParam("info") String param) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            TransLimitRule transLimitRule = JSONObject.parseObject(param, TransLimitRule.class);
            transLimitService.addTransLimitRule(transLimitRule,msg);
        } catch (Exception e){
            log.error("限额新增异常!",e);
            msg.put("status", false);
            msg.put("msg", "限额新增异常!");
        }
        return msg;
    }

    /**
     * 限额修改
     */
    @RequestMapping(value = "/updateTransLimitRule")
    @ResponseBody
    @SystemLog(description = "限额修改",operCode="transLimit.updateTransLimitRule")
    public Map<String,Object> updateTransLimitRule(@RequestParam("info") String param) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            TransLimitRule transLimitRule = JSONObject.parseObject(param, TransLimitRule.class);
            transLimitService.updateTransLimitRule(transLimitRule,msg);
        } catch (Exception e){
            log.error("限额修改异常!",e);
            msg.put("status", false);
            msg.put("msg", "限额修改异常!");
        }
        return msg;
    }

    /**
     * 限额修改状态
     */
    @RequestMapping(value = "/updateTransLimitRuleStatus")
    @ResponseBody
    @SystemLog(description = "限额修改状态",operCode="transLimit.changeTransLimitRuleStatus")
    public Map<String,Object> updateTransLimitRuleStatus(@RequestParam("status") Integer status,@RequestParam("id") Long id) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            transLimitService.updateTransLimitRuleStatus(status,id);
            msg.put("status", true);
            msg.put("msg", "修改成功!");
        } catch (Exception e){
            log.error("限额修改状态异常!",e);
            msg.put("status", false);
            msg.put("msg", "限额修改状态异常!");
        }
        return msg;
    }

    /**
     * 限额删除
     */
    @RequestMapping(value = "/deleteTransLimitRule")
    @ResponseBody
    @SystemLog(description = "限额删除",operCode="transLimit.deleteTransLimitRule")
    public Map<String,Object> deleteTransLimitRule(@RequestParam("id") Long id) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            transLimitService.deleteTransLimitRule(id);
            msg.put("status", true);
            msg.put("msg", "删除成功!");
        } catch (Exception e){
            log.error("限额删除异常!",e);
            msg.put("status", false);
            msg.put("msg", "限额删除异常!");
        }
        return msg;
    }

    /**
     * 导出交易限制管理列表
     */
    @RequestMapping(value="/importTransLimitRuleList")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @SystemLog(description = "导出交易限制管理列表",operCode="transLimit.importTransLimitRule")
    public Map<String, Object> importTransLimitRuleList(@RequestParam("info") String param, HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> msg=new HashMap<String,Object>();
        try {
            TransLimitRule info = JSONObject.parseObject(param, TransLimitRule.class);
            transLimitService.importTransLimitRuleList(info,response);
        }catch (Exception e){
            log.error("导出交易限制记录列表异常!",e);
            msg.put("status", false);
            msg.put("msg", "导出交易限制记录列表异常!");
        }
        return msg;
    }

    @RequestMapping(value="/updateFunctionSwitch")
    @ResponseBody
    @SystemLog(description = "限制总开关修改状态",operCode="transLimit.changeTransLimitRuleStatusAll")
    public  Object updateFunctionSwitch(@RequestParam("functionNumber") String functionNumber,@RequestParam("functionSwitch") Integer functionSwitch )throws Exception{
        Map<String, Object> msg = new HashMap<>();
        try {
            FunctionManager info=functionManagerService.getFunctionManagerByNum(functionNumber);
            info.setFunctionSwitch(functionSwitch);
            int i = functionManagerService.updateFunctionSwitch(info);
            if(i>0){
                FunctionManager fm=functionManagerService.getFunctionManager(info.getId());
                if(FunctionManagerAction.FLUSHFUNCTIONNUMBER.equals(fm.getFunctionNumber())){
                    ClientInterface.flushActivityCache(1);
                }
                if(FunctionManagerAction.FLUSHFUNCTIONNUMBER_BUYREWARD.equals(fm.getFunctionNumber())){
                    ClientInterface.flushActivityCache(1);
                }
                msg.put("status", true);
                msg.put("msg", "设置成功");
            } else {
                msg.put("status", false);
                msg.put("msg", "设置失败");
            }
        } catch (Exception e) {
            msg.put("msg", "服务异常");
            e.printStackTrace();
            msg.put("status", false);
        }
        return msg;
    }

    /**
     * 查询交易限制管理日志
     */
    @RequestMapping(value = "/selectTransLimitRuleLog")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> selectTransLimitRuleLog(@RequestParam("limitRule") Long limitRule) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            List<Map> data=transLimitService.selectTransLimitRuleLog(limitRule);
            msg.put("data",data);
            msg.put("status", true);
        } catch (Exception e){
            log.error("查询交易限制管理列表异常!",e);
            msg.put("status", false);
            msg.put("msg", "查询交易限制管理列表异常!");
        }
        return msg;
    }

    /**
     * 交易限制商户白名单
     */
    @RequestMapping(value = "/selectTransLimitWhiteList")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> selectTransLimitWhiteList(@RequestParam("info") String param, @ModelAttribute("page")
            Page<TransLimitWhite> page) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            TransLimitWhite info = JSONObject.parseObject(param, TransLimitWhite.class);
            transLimitService.selectTransLimitWhiteList(info, page);
            msg.put("page",page);
            msg.put("status", true);
        } catch (Exception e){
            log.error("查询交易限制商户白名单列表异常!",e);
            msg.put("status", false);
            msg.put("msg", "查询交易限制商户白名单列表异常!");
        }
        return msg;
    }

    /**
     * 查询交易限制商户白名单详情
     */
    @RequestMapping(value = "/selectTransLimitWhiteDetail")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    public Map<String,Object> selectTransLimitWhiteDetail(@RequestParam("id") Long id) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            TransLimitWhite data=transLimitService.selectTransLimitWhiteDetail(id);
            msg.put("data",data);
            msg.put("status", true);
        } catch (Exception e){
            log.error("查询交易限制商户白名单详情异常!",e);
            msg.put("status", false);
            msg.put("msg", "查询交易限制商户白名单详情异常!");
        }
        return msg;
    }

    /**
     * 交易限制商户白名单新增
     */
    @RequestMapping(value = "/addTransLimitWhite")
    @ResponseBody
    @SystemLog(description = "交易限制商户白名单新增",operCode="transLimit.addTransLimitWhite")
    public Map<String,Object> addTransLimitWhite(@RequestParam("info") String param) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            TransLimitWhite transLimitWhite = JSONObject.parseObject(param, TransLimitWhite.class);
            if(merchantInfoService.selectByMerNo(transLimitWhite.getMerchantNo())==null){
                msg.put("status", false);
                msg.put("msg", "该商户不存在!");
                return msg;
            }
            if(transLimitService.selectTransLimitWhiteMerCount(transLimitWhite.getMerchantNo(),transLimitWhite.getLimitAction())>0){
                msg.put("status", false);
                msg.put("msg", "该商户已存在交易限制商户白名单!");
                return msg;
            }
            transLimitService.insertTransLimitWhite(transLimitWhite);
            msg.put("status", true);
            msg.put("msg", "新增成功!");
        } catch (Exception e){
            log.error("交易限制商户白名单新增异常!",e);
            msg.put("status", false);
            msg.put("msg", "交易限制商户白名单新增异常!");
        }
        return msg;
    }

    /**
     * 交易限制商户白名单修改
     */
    @RequestMapping(value = "/updateTransLimitWhite")
    @ResponseBody
    @SystemLog(description = "交易限制商户白名单修改",operCode="transLimit.updateTransLimitWhite")
    public Map<String,Object> updateTransLimitWhite(@RequestParam("info") String param) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            TransLimitWhite transLimitWhite = JSONObject.parseObject(param, TransLimitWhite.class);
            if(transLimitService.selectTransLimitWhiteMerCountById(transLimitWhite.getMerchantNo(),transLimitWhite.getLimitAction(),transLimitWhite.getId())>0){
                msg.put("status", false);
                msg.put("msg", "该商户已存在交易限制商户白名单!");
                return msg;
            }
            transLimitService.updateTransLimitWhite(transLimitWhite);
            msg.put("status", true);
            msg.put("msg", "修改成功!");
        } catch (Exception e){
            log.error("交易限制商户白名单修改异常!",e);
            msg.put("status", false);
            msg.put("msg", "交易限制商户白名单修改异常!");
        }
        return msg;
    }

    /**
     * 交易限制商户白名单修改状态
     */
    @RequestMapping(value = "/updateTransLimitWhiteStatus")
    @ResponseBody
    @SystemLog(description = "交易限制商户白名单修改状态",operCode="transLimit.changeTransLimitWhiteStatus")
    public Map<String,Object> updateTransLimitWhiteStatus(@RequestParam("status") Integer status,@RequestParam("id") Long id) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            transLimitService.updateTransLimitWhiteStatus(status,id);
            msg.put("status", true);
            msg.put("msg", "修改成功!");
        } catch (Exception e){
            log.error("交易限制商户白名单修改状态异常!",e);
            msg.put("status", false);
            msg.put("msg", "交易限制商户白名单修改状态异常!");
        }
        return msg;
    }

    /**
     * 交易限制商户白名单删除
     */
    @RequestMapping(value = "/deleteTransLimitWhite")
    @ResponseBody
    @SystemLog(description = "交易限制商户白名单删除",operCode="transLimit.deleteTransLimitWhite")
    public Map<String,Object> deleteTransLimitWhite(@RequestParam("id") Long id) throws Exception{
        Map<String, Object> msg=new HashMap<String,Object>();
        try{
            transLimitService.deleteTransLimitWhite(id);
            msg.put("status", true);
            msg.put("msg", "删除成功!");
        } catch (Exception e){
            log.error("交易限制商户白名单删除异常!",e);
            msg.put("status", false);
            msg.put("msg", "交易限制商户白名单删除异常!");
        }
        return msg;
    }

    /**
     * 导出交易限制商户白名单列表
     */
    @RequestMapping(value="/importTransLimitWhiteList")
    @ResponseBody
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @SystemLog(description = "导出交易限制商户白名单列表",operCode="transLimit.importTransLimitWhite")
    public Map<String, Object> importTransLimitWhiteList(@RequestParam("info") String param, HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> msg=new HashMap<String,Object>();
        try {
            TransLimitWhite info = JSONObject.parseObject(param, TransLimitWhite.class);
            transLimitService.importTransLimitWhiteList(info,response);
        }catch (Exception e){
            log.error("导出交易限制商户白名单列表异常!",e);
            msg.put("status", false);
            msg.put("msg", "导出交易限制商户白名单列表异常!");
        }
        return msg;
    }
}
